package com.example.yichang;

import com.example.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class yichang {
    @ExceptionHandler
    public Result yichangchuli(Exception e){
        log.error("服务器出现异常：",e);
        return Result.error("服务器出现异常");
    }
    @ExceptionHandler
    public Result yichangchuliDuplitcateKey(DuplicateKeyException e){
        log.error("服务器出现异常：",e);
        String message=e.getMessage();
        int i=message.indexOf("Duplicate entry");
        String errMsg=message.substring(i);
        String[] arr=errMsg.split(" ");
        return Result.error(arr[2]+"已存在");
    }
    @ExceptionHandler
    public Result yc(IllegalArgumentException e){
        return Result.error("该部门下有人，不能删除");
    }
}
